//
//  EmptyView.swift
//  GomroClient
//
//  Created by mac on 2019/12/13.
//  Copyright © 2019 cn.gomro.app. All rights reserved.
//

import UIKit

enum EmptyViewType:Int {
    case no_message = 0//没有消息
    case no_record     //没有记录
    case no_network    //没有网络
    case no_order      //没有订单
}

private enum EmptyViewTypeString:String {
    case no_message = "没有消息"
    case no_record = "没有记录"
    case no_network = "服务器开了小差~~，点我重试"
    case no_order = "没有订单"
}

private enum EmptyViewTypeImage:String {
    case no_message = "empty_no_message"
    case no_record = "empty_no_record"
    case no_network = "empty_no_network"
    case no_order = "empty_no_order"
}


/** 空视图 */
class EmptyView: UIView {

    /** 无网络点击刷新 */
    var noNetworkRefreshBlock:(()->(Void))?
    
    static let k_width:CGFloat = 205.5
    static let k_height:CGFloat = 185.5
    
    lazy var defaultType:EmptyViewType = .no_message
    var ctype:EmptyViewType? {
        didSet{
            
            if AppDelegate.app().isNetWork() == false {
                self.imgInfo.image = YYImage.init(named: EmptyViewTypeImage.no_network.rawValue)
                self.labInfo.text = EmptyViewTypeString.no_network.rawValue
            }
            else{
                switch self.ctype {
                   case .no_message:
                    self.imgInfo.image = YYImage.init(named: EmptyViewTypeImage.no_message.rawValue)
                    self.labInfo.text = EmptyViewTypeString.no_message.rawValue
                       
                   case .no_record:
                    self.imgInfo.image = YYImage.init(named: EmptyViewTypeImage.no_record.rawValue)
                    self.labInfo.text = EmptyViewTypeString.no_record.rawValue
                       
                   case .no_network:
                    self.imgInfo.image = YYImage.init(named: EmptyViewTypeImage.no_network.rawValue)
                    self.labInfo.text = EmptyViewTypeString.no_network.rawValue
                       
                   case .no_order:
                    self.imgInfo.image = YYImage.init(named: EmptyViewTypeImage.no_order.rawValue)
                    self.labInfo.text = EmptyViewTypeString.no_order.rawValue
                case .none:
                    break
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateConstraints() {
        
        self.imgInfo.snp.makeConstraints { make in
            make.top.left.right.equalTo(0)
            make.height.equalTo(144)
            make.width.equalTo(self.snp.width)
        }
        
        self.labInfo.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(21)
        }
        
        super.updateConstraints()
    }
    
    
    //MARK: - initView
    private func initView(){
        self.backgroundColor = UIColor.clear
        
        self.addSubview(self.imgInfo)
        self.addSubview(self.labInfo)
        
        if AppDelegate.app().isNetWork() == false {
            self.ctype = .no_network
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(networkNoticeAction(sender:)),
                                               name: NSNotification.Name(rawValue: K_APP_IS_NET_WORK),
                                               object: nil)
        
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tapAction(sender:)))
        self.addGestureRecognizer(tapGesture)
        self.isUserInteractionEnabled = true
        
        setNeedsUpdateConstraints()
    }
    
    @objc private func networkNoticeAction(sender:Notification) {
        guard let _userInfo = sender.userInfo as? [String:Any] else {
            return
        }
        
        let code:Int? = _userInfo[K_APP_IS_NET_WORK_CODE] as? Int
        
        //无网
        if code == 404 {
            self.ctype = .no_network
        }
        else {
            //有网，还原到之前默认的状态
            self.ctype = self.defaultType
        }
    }
    
    @objc private func tapAction(sender:UITapGestureRecognizer) {
        self.noNetworkRefreshBlock?()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self,
                                                  name: NSNotification.Name(rawValue: K_APP_IS_NET_WORK),
                                                  object: nil)
        
        print("EmptyView 销毁了")
    }
    
    
    //MARK: - lazy load
    private lazy var imgInfo:UIImageView = {
        let _img:UIImageView = UIImageView.init(image: YYImage.init(named: EmptyViewTypeImage.no_message.rawValue))
        
        return _img
    }()
    
    private lazy var labInfo:YYLabel = {
        let _lab = BaseView.createLable(rect: CGRect.zero,
                               text: EmptyViewTypeString.no_message.rawValue,
                               textColor: UIColor.init().colorFromHexString(hex: "#999999"),
                               font: UIFont.systemFont(ofSize: 15, weight: .bold),
                               backgroundColor: UIColor.clear)
        _lab.textAlignment = .center
        return _lab
    }()
}

